package com.starblink.search.category.ui.goodslist

import android.graphics.Color
import android.view.MotionEvent
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.FragmentUtils
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.extension.extraOf
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.basic.route.RoutePage
import com.starblink.search.config.SearchConfig
import com.starblink.search.databinding.ActivityCategoryGoodsListBinding
import com.starblink.search.result.ui.fragment.goods.GoodsResultFragment

@Route(path = RoutePage.Search.PAGE_CATEGORY_GOODS)
internal class CategoryGoodsActivity :
    BaseTVMActivity<ActivityCategoryGoodsListBinding, BaseViewModel>() {

    @JvmField
    @Autowired(name = RoutePage.ARG.ID)
    var categoryId: String = ""

    @JvmField
    @Autowired(name = RoutePage.ARG.TITLE)
    var title: String = ""

    @JvmField
    @Autowired(name = RoutePage.ARG.IS_SALE_ZONE)
    var isSaleZone: Boolean = false

    private var resultFragment: GoodsResultFragment? = null

    override fun initViewBinding(): ActivityCategoryGoodsListBinding =
        ActivityCategoryGoodsListBinding.inflate(layoutInflater)

    override fun initView() {
        super.initView()
        setStatusBarColor(Color.WHITE)
        viewBinding.titleBar.leftView.setOnClickListener {
            popSelf()
        }
        viewBinding.titleBar.rightView.setOnClickListener {
            navigationTo(url = RoutePage.Search.PAGE_SEARCH) {
                extraOf(RoutePage.Search.ARG_SCENE to RoutePage.Search.SCENE.GOODS.value)
            }
        }
        viewBinding.titleBar.title = title

        resultFragment = GoodsResultFragment.newInstance(
            keyword = categoryId,
            categoryName = title,
            scene = SearchConfig.SCENE_CATEGORY,
            isSaleZone = isSaleZone,
            pageTrack = false
        )
        FragmentUtils.add(
            supportFragmentManager,
            resultFragment!!,
            viewBinding.container.id,
            false,
            false
        )
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_UP) {
            resultFragment?.onParentViewTouch(ev)
        }
        return super.dispatchTouchEvent(ev)
    }

    override fun onResume() {
        super.onResume()
        pageExposure(
            SpmPageDef.CategoryGoodsPage,
            params = mapOf(
                "searchTerm" to title,
                "categoryId" to categoryId,
            )
        )
    }

    override fun onPause() {
        super.onPause()
        pageDisappear(
            SpmPageDef.CategoryGoodsPage,
            params = mapOf(
                "searchTerm" to title,
                "categoryId" to categoryId,
            )
        )
    }

}